import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

final networkUriRef = RegExp("^https?://");
final localUriRef = RegExp("^static/");

class CommonImageWidget extends StatelessWidget {
  static const _cacheKey = 'customCacheKey';
  final String src;
  final double? width;
  final double? height;
  final BoxFit? fit;

  const CommonImageWidget(
    this.src, {
    super.key,
    this.width,
    this.height,
    this.fit,
  });

  @override
  Widget build(BuildContext context) {
    if (networkUriRef.hasMatch(src)) {
      return CachedNetworkImage(
        imageUrl: src,
        fit: fit,
        width: width,
        height: height,
        // placeholder: (context, url) => const CircularProgressIndicator(),
        errorWidget: (context, url, error) => const Icon(Icons.error),
        // cacheManager: CacheManager(
        //   Config(
        //     _cacheKey,
        //     stalePeriod: const Duration(days: 7),
        //     fileSystem: IOFileSystem(_cacheKey),
        //     fileService: HttpFileService(),
        //     repo: JsonCacheInfoRepository(databaseName: _cacheKey),
        //     maxNrOfCacheObjects: 100,
        //   ),
        // ),
      );
    }
    if (localUriRef.hasMatch(src)) {
      return Image.asset(
        src,
        width: width,
        height: height,
        fit: fit,
      );
    }
    assert(false, 'Unknown image source');
    return const Placeholder();
  }
}
